<?php

namespace app\api\controller\fishing;

use app\admin\model\FishingAcc;
use app\admin\model\FishingFishpond;
use app\common\controller\Api;
use think\Cache;
use think\Request;

/**
 * 天天去钓鱼鱼塘相关接口
 */
class Fishpond extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public $openid;

    // 买鱼倍数
    public $buy_fish_rate = 5;
    // 离线收益计算时间
    public $base_offline = 1800;
    // 鱼塘容量上限
    public $max_pond_count = 30;

    public function __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin:*');
        parent::__construct($request);
        $this->openid = $request->post('openid', 'mnq') ?: 'mnq';
    }

    // 根据json获取鱼价格
    public function get_fish_price($fish_id)
    {
        $fish_price = Cache::get('fish_price');
        // 判断是否又缓存,否则读取json数据
        if (!$fish_price) {
            $filename = ROOT_PATH . 'public/assets/json/fishing/fish_define.json';
            $fish_price = json_decode(file_get_contents($filename), true);
            Cache::set('fish_price', $fish_price);
        }
        return $fish_price[$fish_id]['price'];
    }

    // 升级鱼塘容量(扩建)
    public function update_pond_count()
    {
        $acc = FishingAcc::get(['openid' => $this->openid]);
        $acc->fishpond_count += 1;
        // 判断是否到达容量上限
        if ($acc->fishpond_count >= $this->max_pond_count) {
            $this->result('fail', 'exceeds limit');
        }

        if ($acc->save() !== false) {
            $this->result('ok', $acc->fishpond_count);
        } else {
            $this->result('fail', 'fail');
        }
    }

    // 获取小时收益(聚宝盆)
    public function get_hours_money()
    {
        $hours = $this->request->post('hours') ?: 1;
        $money = $this->get_fishpond_income() * $hours;
        $acc = FishingAcc::get(['openid' => $this->openid]);
        $acc->money += $money;
        if ($acc->save() !== false) {
            $this->result('ok', $acc->money);
        } else {
            $this->result('fail', 'fail');
        }
    }

    // 增加鱼塘收益加成(投食)
    public function add_fishpond_rate()
    {
        $acc = FishingAcc::get(['openid' => $this->openid]);
        $acc->fishpond_rate += 0.1;
        if ($acc->save() !== false) {
            $data['fishpond_rate'] = $acc->fishpond_rate;
            $data['fishpond_income'] = $this->get_fishpond_income();
            $this->result('ok', $data);
        } else {
            $this->result('fail', 'fail');
        }
    }

    // 获取鱼塘中有的鱼和其收益
    public function get_has_fish()
    {
        // 加成
        $rate = FishingAcc::where(['openid' => $this->openid])->value('fishpond_rate') + 1;
        $all = FishingFishpond::all(['openid' => $this->openid]);
        // 判断是否有鱼
        if ($all) {
            // 遍历获取每条鱼的收益
            foreach ($all as $fish) {
                $income = floor($this->get_offline_income($fish) * $rate);
                $fish->income = $income;
            }
            $this->result('', $all);
        } else {
            $this->result('', []);
        }
    }

    // 买鱼
    public function buy_fish()
    {
        $fish_id = $this->request->post('fish_id');

        // 获取鱼的价格
        $price = $this->get_fish_price($fish_id);

        $acc = FishingAcc::get(['openid' => $this->openid]);
        $count = FishingFishpond::where(['openid' => $this->openid])->count();

        // 判断是否能购买
        $collection = explode('_', $acc->collection);
        if (!in_array($fish_id, $collection)) {
            $this->result('fail', 'no fish');
        }

        // 判断是否到达鱼塘容量上限
        if ($count > $acc->fishpond_count) {
            $this->result('fail', 'exceeds limit');
        } else {
            // 判断金钱是否足够买鱼
            if ($acc->money < $price * $this->buy_fish_rate) {
                $this->result('fail', 'no money');
            }

            // 新建鱼塘鱼
            $fish = new FishingFishpond();
            $fish->openid = $this->openid;
            $fish->fish_id = $fish_id;
            $fish->price = $price;
            $createtime = time();
            $fish->createtime = $createtime;

            // 扣钱
            $acc->money -= $price * $this->buy_fish_rate;

            if ($acc->save() !== false && $fish->save() !== false) {
                $data['money'] = $acc->money;
                $data['fish_id'] = $fish_id;
                $data['createtime'] = $createtime;
                $data['fishpond_income'] = $this->get_fishpond_income();
                $this->result('ok', $data);
            }
            $this->result('fail', 'fail');
        }
    }

    // 卖鱼
    public function sale_fish()
    {
        $fish_id = $this->request->post('fish_id');
        $createtime = $this->request->post('createtime');

        $acc = FishingAcc::get(['openid' => $this->openid]);
        $rate = $acc->fishpond_rate; // 加成

        $income = 0;
        // 判断是否卖出全部
        if ($fish_id == 'all') {
            $fishs = FishingFishpond::all(['openid' => $this->openid]);
            foreach ($fishs as $fish) {
                $income += $this->get_offline_income($fish);
                $fish->delete();
            }
        } else {
            $fish = FishingFishpond::get(['openid' => $this->openid, 'fish_id' => $fish_id, 'createtime' => $createtime]);
            if ($fish) {
                $income = $this->get_offline_income($fish);
                $fish->delete();
            }
        }

        $acc->money += floor($income * $rate);
        $res = $acc->save();
        if ($res !== false) {
            $data['fishpond_income'] = $this->get_fishpond_income();
            $data['money'] = $acc->money;
            $this->result('money', $data);
        } else {
            $this->result('fail', 'fail');
        }
    }

    // 获取鱼塘每小时收益
    public function get_fishpond_income()
    {
        $rate = FishingAcc::where(['openid' => $this->openid])->value('fishpond_rate') + 1;
        return floor(FishingFishpond::where(['openid' => $this->openid])->sum('price') * 2 * $rate);
    }

    // 计算鱼塘离线收益
    public function get_offline_income($fish)
    {
        $income = 0;
        $createtime = $fish->createtime;
        if ($createtime) {
            $offtime = floor((intval(time()) - $createtime) / $this->base_offline);
            $income = $fish->price * $offtime;
        }
        return $income;
    }
}
